package edu.ruc.cluster.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

/**
 * 2012-12-10
 * FileTransfer: used to transfer file from src--->dst on host with username and passwd
 * Called example:
 * 		FileTransfer ft = new FileTransfer("dlnode27", "deke", "111111");
 * 		ft.transform("/usr/a.txt", "/home/deke");
 * @author deke
 */
public class FileTransfer {

	private static final DefaultLogHandler logHandler = DefaultLogHandler.getInstance();
	Map<String, String> sftpDetails = null;
	private String src = null;
	private String dst = null;
	
	public SFTPChannel getSFTPChannel() {
        return new SFTPChannel();
    }

	public FileTransfer(String host, String username, String passwd){
		sftpDetails = new HashMap<String, String>();
		
		//setup host ip， port, username, password
        sftpDetails.put(Const.SFTP_REQ_HOST, host);
        sftpDetails.put(Const.SFTP_REQ_USERNAME, username);
        sftpDetails.put(Const.SFTP_REQ_PASSWORD, passwd);
        sftpDetails.put(Const.SFTP_REQ_PORT, "22");
       
	}
    /**
     * @param args
     * @throws Exception
     */
    private void ftp(){
    	
    	try{
    		SFTPChannel channel = getSFTPChannel();
    		ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000);
    		File file = new File(src);
    		long fileSize = file.length();
    		    		
    		/**
             * code segment 1
            OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE); // OVERWRITE mode
            byte[] buff = new byte[1024 * 256]; // buffer transfer per time : 256KB
            int read;
            if (out != null) {
                System.out.println("Start to read input stream");
                InputStream is = new FileInputStream(src);
                do {
                    read = is.read(buff, 0, buff.length);
                    if (read > 0) {
                        out.write(buff, 0, read);
                    }
                    out.flush();
                } while (read >= 0);
                System.out.println("input stream read done.");
            }
            **/
    		System.out.println(chSftp.pwd());
    			// dst should be relative to current remote directory
//            chSftp.mkdir(dst);
    		// note this method can only transform file, not directory
            chSftp.put(src, dst, new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE); // code segment 2
            
//             chSftp.put(new FileInputStream(src), dst, ChannelSftp.OVERWRITE); // code segment 3
            
            chSftp.quit();
            channel.closeChannel();
            
    	}catch(JSchException e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}catch(SftpException e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}catch(Exception e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}
    }
    
    public void transform(String source, String destination){
    	
    	if(source == null || destination == null || source.equals("") || destination.equals("")){
    		logHandler.error(this.getClass().getSimpleName(), new NullPointerException());
    		return;
    	}
    	src = source;
    	dst = destination; 	
        ftp();
    }
    
    public static void main(String[] args){
    	String host = "dlnode13";
    	String username = "deke";
    	String passwd = "111111";
    	FileTransfer ft = new FileTransfer(host, username, passwd);
    	ft.transform("core-site.xml", "/home/deke/");
    }
}
